Telegram Group & Telegram Channel
📚 Организация middleware в Go без сторонних зависимостей

Если ты пишешь веб-приложения на Go и хочешь избавиться от внешних зависимостей вроде alice, статья от Алекса Эдвардса — must read:
https://www.alexedwards.net/blog/organize-your-go-middleware-without-dependencies

📌 В чем суть:

• Проблема: стандартная библиотека Go не даёт удобного способа цепочечного подключения middleware
• Цель: избежать дублирования кода и сделать middleware масштабируемыми без сторонних пакетов

💡 Решение — создать собственный тип chain, который позволяет «наматывать» middleware на обработчики:


type chain []func(http.Handler) http.Handler

func (c chain) then(h http.Handler) http.Handler {
for i := len(c) - 1; i >= 0; i-- {
h = c[i](h)
}
return h
}

func (c chain) thenFunc(h http.HandlerFunc) http.Handler {
return c.then(h)
}


Теперь ты можешь описывать цепочки middleware так:


mux := http.NewServeMux()

baseChain := chain{requestID, logRequest}
adminChain := append(baseChain, authenticateUser, requireAdminUser)

mux.Handle("GET /", baseChain.thenFunc(home))
mux.Handle("GET /admin", adminChain.thenFunc(showAdminDashboard))


Этот подход:
• Простой
• Без зависимостей
• Легко расширяется

Полная статья и объяснения тут:

@golang_books
Please open Telegram to view this post
VIEW IN TELEGRAM



tg-me.com/golang_books/975
Create:
Last Update:

📚 Организация middleware в Go без сторонних зависимостей

Если ты пишешь веб-приложения на Go и хочешь избавиться от внешних зависимостей вроде alice, статья от Алекса Эдвардса — must read:
https://www.alexedwards.net/blog/organize-your-go-middleware-without-dependencies

📌 В чем суть:

• Проблема: стандартная библиотека Go не даёт удобного способа цепочечного подключения middleware
• Цель: избежать дублирования кода и сделать middleware масштабируемыми без сторонних пакетов

💡 Решение — создать собственный тип chain, который позволяет «наматывать» middleware на обработчики:


type chain []func(http.Handler) http.Handler

func (c chain) then(h http.Handler) http.Handler {
for i := len(c) - 1; i >= 0; i-- {
h = c[i](h)
}
return h
}

func (c chain) thenFunc(h http.HandlerFunc) http.Handler {
return c.then(h)
}


Теперь ты можешь описывать цепочки middleware так:


mux := http.NewServeMux()

baseChain := chain{requestID, logRequest}
adminChain := append(baseChain, authenticateUser, requireAdminUser)

mux.Handle("GET /", baseChain.thenFunc(home))
mux.Handle("GET /admin", adminChain.thenFunc(showAdminDashboard))


Этот подход:
• Простой
• Без зависимостей
• Легко расширяется

Полная статья и объяснения тут:

@golang_books

BY Golang Books


Warning: Undefined variable $i in /var/www/tg-me/post.php on line 283

Share with your friend now:
tg-me.com/golang_books/975

View MORE
Open in Telegram


Golang Books Telegram | DID YOU KNOW?

Date: |

NEWS: Telegram supports Facetime video calls NOW!

Secure video calling is in high demand. As an alternative to Zoom, many people are using end-to-end encrypted apps such as WhatsApp, FaceTime or Signal to speak to friends and family face-to-face since coronavirus lockdowns started to take place across the world. There’s another option—secure communications app Telegram just added video calling to its feature set, available on both iOS and Android. The new feature is also super secure—like Signal and WhatsApp and unlike Zoom (yet), video calls will be end-to-end encrypted.

Mr. Durov launched Telegram in late 2013 with his brother, Nikolai, just months before he was pushed out of VK, the Russian social-media platform he founded. Mr. Durov pitched his new app—funded with the proceeds from the VK sale—less as a business than as a way for people to send messages while avoiding government surveillance and censorship.

Golang Books from tw


Telegram Golang Books
FROM USA